package com.eop.managedBean;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.form.impl.UICombo;
import org.operamasks.faces.user.util.Browser;

import com.base.attachment.entity.Attachment;
import com.base.attachment.service.AttachmentException;
import com.base.attachment.service.IAttachmentStorer;
import com.base.attachment.service.impl.AttachmentService;
import com.base.common.provider.CommonKeySelectItemProvider;
import com.base.commons.SpringBeanHolder;
import com.base.utils.IdUtils;
import com.eop.entity.Schnario;

@ManagedBean(name = "eop_addNewVerSchBean", scope = ManagedBeanScope.REQUEST)
public class AddNewVerSchBean extends BaseSchnarioBean {
	private AttachmentService attachmentServcie = SpringBeanHolder
			.getBean("attachmentSrv");
	private IAttachmentStorer attachmentStorer = SpringBeanHolder
			.getBean("attachmentStorer");
	@Inject
	private FacesContext context;

	@SaveState
	private String first;

	@Bind(id = "srRangeBigType", attribute = "binding")
	private UICombo srRangeBigType_binding;

	@Bind(id = "srRangeThinType", attribute = "binding")
	private UICombo srRangeThinType_binding;

	@SaveState
	private String superId;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			superId = context.getExternalContext().getRequestParameterMap()
					.get("id");
			id = IdUtils.getUUID();
			if (!"".equals(superId) && null != superId) {
				Schnario model = schnarioService.getSchById(superId);
				if (model != null) {
					srRangeThinType_items = CommonKeySelectItemProvider
							.getDirectCommonKey(model.getSrRangeBigType(),
									false);
					getModelValue(model);
				}
			}

		}

	}

	@Action(id = "srRangeBigType", event = "onselect", immediate = true)
	public void changeSrRangeBigType() {
		first = srRangeBigType_binding.getSubmittedValue().toString().trim();
		srRangeThinType_items = CommonKeySelectItemProvider.getDirectCommonKey(
				first, false);
		srRangeThinType = null;
		srRangeThinType_binding.resetValue();
		srRangeThinType_binding.repaint();

	}

	private void getModelValue(Schnario sch) {
		srCode = sch.getSrCode();
		srName = sch.getSrName();
		model = sch.getModel();
		remark = sch.getRemark();
		srRangeBigType = sch.getSrRangeBigType();
		srRangeThinType = sch.getSrRangeThinType();
		srType = sch.getSrType();
		createUser = sch.getCreateUser();
		createDate = sch.getCreateDate();
		version=sch.getVersion();
		srVersion = this.editionCodeValue();
	}

	public String editionCodeValue() {
		String code = "1.0";
		if (superId != null && !"".equals(superId)) {
			Schnario model = schnarioService.getSchById(superId);
			String newCode = model.getSrVersion();
			if (newCode != null && !"".equals(newCode)) {
				code = (Integer.parseInt(newCode.substring(0, newCode
						.indexOf("."))) + 1)
						+ newCode.substring(newCode.indexOf("."));
			}
		}
		return code;
	}

	@Action(id = "save")
	public void save() {
		sr = new Schnario();
		Schnario model = schnarioService.getSchById(superId);
		sr.setSrVersion(srVersion);
		sr.setSrLastSchnario(superId);
		// 复制文本信息
		sr.setContent(model.getContent());
		this.setSchnarioProperty();

		model.setEffect(false);
		model.setScrapTime(new Date());
		schnarioService.saveSchnario(sr);
		schnarioService.saveSchnario(model);

		// 同时复制附件和流程
		this.copyAtt(model, sr);

		String url = "/modules/eop/showSchnario.faces?id=" + sr.getId();
		url = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestContextPath()
				+ url;
		String script = "addCenterLayoutTab('showSchnarioTab','查看预案','" + url
				+ "')";
		Browser.execClientScript("window.top", script);
		Browser
				.execClientScript("javascript:closeCenterLayoutTab('createNewSch')");

	}

	public void copyAtt(Schnario sourceSch, Schnario destSch) {
		try {
			List<Attachment> attList = attachmentServcie
					.getAttachmentsByBeanAndType(Schnario.class.getName(),
							sourceSch.getId(), "");
			if (attList != null && attList.size() > 0) {
				for (int i = 0; i < attList.size(); i++) {
					Attachment att = attList.get(i);

					Attachment attachment = new Attachment();
					attachment.setAuthor(att.getAuthor());
					attachment.setBeanClass(att.getBeanClass());
					attachment.setBeanId(destSch.getId());
					attachment.setCreateTime(att.getCreateTime());
					attachment.setFileName(att.getFileName());
					attachment.setFileExt(att.getFileExt());
					attachment.setLength(att.getLength());
					attachment.setAttachmentType(att.getAttachmentType());
					InputStream is = attachmentStorer
							.downloadAttachment(attList.get(i));
					attachmentServcie.createAttachment(attachment, is, false);
				}
			}
		} catch (AttachmentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
